using System;
using System.Linq.Expressions;
using FwkLight.Domain;

namespace FwkLight.CodeExtensions
{
  public static class ObjectExtensions
  {
    public static string ToNullSafeString(this object value)
    {
      return value == null ? "" : value.ToString();
    }

    public static ReturnType CalculateThe<EntityType, ReturnType>(this EntityType item,
                                                                  ICalculator<ReturnType, EntityType> calculator)
    {
      return calculator.Calculate(item);
    }

    public static ReturnType CalculateThe<EntityType, ReturnType, Type2, Type3>(this EntityType item,
                                                                                ICalculator
                                                                                  <ReturnType, EntityType, Type2, Type3>
                                                                                  calculator, Type2 param2, Type3 param3)
    {
      return calculator.Calculate(item, param2, param3);
    }

    public static ReturnType CalculateThe<EntityType, ReturnType, Type2>(this EntityType item,
                                                                         ICalculator<ReturnType, EntityType, Type2>
                                                                           calculator, Type2 param2)
    {
      return calculator.Calculate(item, param2);
    }

    public static string GetFieldName<T, R>(this T item, Expression<Func<T, R>> property)
    {
      return ReflectionHelper.GetProperty(property).Name;
    }

		public static void Do<T>(this T item, IDomainAction<T> action)
		{
			action.DoAction(item);
		}

		public static void Do<T, Param1>(this T item, IDomainAction<T, Param1> action, Param1 param1)
		{
			action.DoAction(item, param1);
		}

		public static void Do<T, Param1, Param2>(this T item, IDomainAction<T, Param1, Param2> action, Param1 param1, Param2 param2)
		{
			action.DoAction(item, param1, param2);
		}

		public static void Do<T, Param1, Param2, Param3>(this T item, IDomainAction<T, Param1, Param2, Param3> action, Param1 param1, Param2 param2, Param3 param3)
		{
			action.DoAction(item, param1, param2, param3);
		}

		public static void Do<T, Param1, Param2, Param3, Param4>(this T item, IDomainAction<T, Param1, Param2, Param3, Param4> action, Param1 param1, Param2 param2, Param3 param3, Param4 param4)
		{
			action.DoAction(item, param1, param2, param3, param4);
		}
  }
}